package operation

import (
	"../common"
	"../web"
	"math"
	"strconv"
	"strings"
)

type OperPimPassiveObj struct {
	common.BaseHandler
}

func (self *OperPimPassiveObj) Prepare() {
	wono := strings.TrimSpace(self.Ctx.Params["wono"])
	line := strings.TrimSpace(self.Ctx.Params["line"])
	item := strings.TrimSpace(self.Ctx.Params["item"])
	station := strings.TrimSpace(self.Ctx.Params["station"])
	freqy := strings.TrimSpace(self.Ctx.Params["freqy"])
	limit := strings.TrimSpace(self.Ctx.Params["limitval"])

	self.TmplData["line"] = line
	self.TmplData["wono"] = wono
	self.TmplData["item"] = item
	self.TmplData["station"] = station
	self.TmplData["freqy"] = freqy
	self.TmplData["limitval"] = limit
	s_band := strings.Split(freqy, ",")
	self.TmplData["band"] = s_band
}

func (self *OperPimPassiveObj) ParameterCheck() bool {
	s_ssn := strings.TrimSpace(self.Ctx.Params["ssn"])
	freqy := self.TmplData["freqy"].(string)
	s_band := strings.Split(freqy, ",")
	list_val := make(map[string][]string)
	list_rst := make(map[string]string)
	count := 0
	final_result := "PASS"
	for _, v := range s_band {
		list_rst[v] = "PASS"
		count = count + 1
	}
	if self.TmplData["wono"].(string) == "" {
		common.ForbiddenHandler(self.Ctx)
		return false
	}
	if s_ssn == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Please input the Product No.")
		return false
	}
	for _, v := range s_band {
		value := strings.TrimSpace(self.Ctx.Params[v])
		val := strings.Split(value, ",")
		for i, local := range val {
			_, err := strconv.ParseFloat(strings.TrimSpace(local), 32)
			if err != nil {
				if len(val) == 1 {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] must be a numberic.", v)
				} else {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] Index %d must be a numberic.", v, i+1)
				}
				return false
			}
		}

		list_val[v] = val
	}
	limit := self.TmplData["limitval"].(string)
	item := self.TmplData["item"].(string)
	if limit == "" {
		self.TmplData["error"] = common.GetText(self.Lang, "Item [%s] Limit didnt set.", item)
		return false
	}
	all_limit := strings.Split(limit, ",")
	if len(all_limit) == 1 {
		f_limit, _ := strconv.ParseFloat(strings.TrimSpace(limit), 32)
		for k, v := range list_val {
			for i, val := range v {
				f_val, _ := strconv.ParseFloat(strings.TrimSpace(val), 32)
				f_64, _ := strconv.ParseFloat(strings.TrimSpace(val), 64)
				if math.Abs(f_64) > 150.0 {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] index %d test data can not be less than -150.0", k, i+1)
					return false
				}
				if f_64 > 0.0 {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] index %d test data can not be more than 0.0", k, i+1)
					return false
				}
				if f_val > f_limit {
					list_rst[k] = "FAIL"
					final_result = "FAIL"
					break
				}
			}
		}
	} else {
		for k, v := range list_val {
			if len(v) > 0 && len(all_limit) != len(v) {
				self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] setting does not match the number of test data.", item, k)
				return false
			}
		}
		for k, v := range list_val {
			for i, val := range v {
				f_val, _ := strconv.ParseFloat(strings.TrimSpace(val), 32)
				lval, _ := strconv.ParseFloat(strings.TrimSpace(all_limit[i]), 32)
				f_64, _ := strconv.ParseFloat(strings.TrimSpace(val), 64)
				if math.Abs(f_64) > 150.0 {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] index %d test data can not be less than -150.0", k, i+1)
					return false
				}
				if f_64 > 0.0 {
					self.TmplData["error"] = common.GetText(self.Lang, "[%sMHz] index %d test data can not be more than 0.0", k, i+1)
					return false
				}
				if f_val > lval {
					list_rst[k] = "FAIL"
					final_result = "FAIL"
					break
				}
			}
		}
	}

	self.Sqlparas = append(self.Sqlparas, s_ssn)
	self.Sqlparas = append(self.Sqlparas, self.TmplData["wono"])
	self.Sqlparas = append(self.Sqlparas, self.TmplData["station"])
	self.Sqlparas = append(self.Sqlparas, self.TmplData["line"])
	self.Sqlparas = append(self.Sqlparas, item)
	self.Sqlparas = append(self.Sqlparas, limit)
	self.Sqlparas = append(self.Sqlparas, list_val)
	self.Sqlparas = append(self.Sqlparas, list_rst)
	self.Sqlparas = append(self.Sqlparas, final_result)
	self.Sqlparas = append(self.Sqlparas, self.Usr.Username)
	self.Sqlparas = append(self.Sqlparas, count)
	self.Flashparas = []interface{}{strings.ToUpper(s_ssn)}

	return true
}

func (self *OperPimPassiveObj) SqlExecute() bool {
	serial := self.Sqlparas[0].(string)
	wono := self.Sqlparas[1].(string)
	station := self.Sqlparas[2].(string)
	line := self.Sqlparas[3].(string)
	item := self.Sqlparas[4].(string)
	limit := self.Sqlparas[5].(string)
	values := self.Sqlparas[6].(map[string][]string)
	results := self.Sqlparas[7].(map[string]string)
	count := self.Sqlparas[10].(int) - 1
	users := self.Sqlparas[9].(string)
	final := make([]string, count)
	for i := 0; i <= count; i++ {
		final = append(final, "")
	}
	final[count] = self.Sqlparas[8].(string)

	num := 0
	for k, v := range values {
		val := strings.Join(v, ",")
		spRtnData := *common.FetchOneRowPtr(
			`sp_TestDataForPassivePim ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?`,
			self.Usr.Conn, new(common.SPReturn), serial, wono, station, line, item, limit, k, val,
			results[k], final[num], users)
		if spRtnData.(common.SPReturn).Flag > 0 {
			self.TmplData["error"] = common.GetTextSP(self.Lang, spRtnData)
			return false
		}
		num = num + 1
	}

	return true
}

func PassivePimHandler(ctx *web.Context) {
	self := new(OperPimPassiveObj)
	self.Ctx = ctx
	self.Html = "passivePim.html"
	self.Rightlist = []string{"scanflow"}
	self.Flashstr = "Save [ %v ] finished."
	self.DoRender(self)
}
